package com.xnyzc.lhy.common.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.xnyzc.lhy.common.penum.cms.EMenuShow;
import com.xnyzc.lhy.common.penum.dept.EOffIceType;
import com.xnyzc.lhy.mis.entity.area.OaSysArea;
import com.xnyzc.lhy.mis.entity.cms.OaSysMenu;
import com.xnyzc.lhy.mis.entity.sidebar.OaSysOffice;
import com.xnyzc.lhy.mis.entity.vehicle.OaSysVehicleMdict;
import org.apache.commons.lang.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * @Description: 树结构处理
 * @Author: wangshuo
 * @Date: 2019/8/19 12:04
 */
public class ListToTreeUtil {

    /**
     - listToTree
     - <p>方法说明<p>
     - 将JSONArray数组转为树状结构
     - @param arr 需要转化的数据
     - @param id 数据唯一的标识键值
     - @param pId 父id唯一标识键值
     - @param child 子节点键值
     - @return JSONArray
     */
    public static JSONArray listToTree(JSONArray arr, String value, String pId, String child){
        JSONArray r = new JSONArray();
        JSONObject hash = new JSONObject();
        //将数组转为Object的形式，key为数组中的id
        for(int i=0;i<arr.size();i++){
            JSONObject json = (JSONObject) arr.get(i);
            hash.put(json.getString(value), json);
        }
        //遍历结果集
        for(int j=0;j<arr.size();j++){
            //单条记录
            JSONObject aVal = (JSONObject) arr.get(j);
            //在hash中取出key为单条记录中pId的值
            JSONObject hashVP = (JSONObject) hash.get(aVal.get(pId).toString());
            //如果记录的pId存在，则说明它有父节点，将她添加到孩子节点的集合中
            if(hashVP!=null){
                //检查是否有child属性
                if(hashVP.get(child)!=null){
                    JSONArray ch = (JSONArray) hashVP.get(child);
                    ch.add(aVal);
                    hashVP.put(child, ch);
                }else{
                    JSONArray ch = new JSONArray();
                    ch.add(aVal);
                    hashVP.put(child, ch);
                }
            }else{
                r.add(aVal);
            }
        }
        return r;
    }

    /**
     * 机构树列表
     * @param list
     * @return
     */
    public static List<Map<String, Object>> getOfficeList(List<OaSysOffice> list,List<OaSysArea> oaSysAreaList,String extId){
        List<Map<String, Object>> officeMapList = Lists.newArrayList();
        for (int i=0; i<list.size(); i++){
            OaSysOffice e = list.get(i);
            Map<String, Object> resMap = Maps.newHashMap();
            if (StringUtils.isBlank(extId) || (extId!=null && !extId.equals(e.getOfficeId()) && e.getParentIds().indexOf(","+extId+",")==-1)){
                resMap.put("id", e.getOfficeId());
                resMap.put("pId", e.getParentId());
                resMap.put("name", e.getName());
                resMap.put("code",e.getCode());
                // 1公司、2部门、3小组、4其他
                if(CheckUtil.objIsNotEmpty(e.getType())){
                    resMap.put("typeStr",EOffIceType.findById(e.getType()).getDesc());
                }
                for(OaSysArea area:oaSysAreaList){
                    if(e.getAreaId().equals(String.valueOf(area.getAreaId()))){
                        resMap.put("areaName",area.getName());
                        resMap.put("areaId",area.getAreaId());
                    }
                }
                officeMapList.add(resMap);
            }
        }
        return officeMapList;
    }

    /**
     * 菜单树列表
     * @param list
     * @return
     */
    public static List<Map<String, Object>> getMenuList(List<OaSysMenu> list){
        List<Map<String, Object>> menuMapList = Lists.newArrayList();
        for (int i=0; i<list.size(); i++){
            OaSysMenu e = list.get(i);
            Map<String, Object> resMap = Maps.newHashMap();
            resMap.put("id", e.getMenuId());
            resMap.put("pId", e.getParentId());
            resMap.put("name", e.getName());
            resMap.put("href",e.getHref());
            resMap.put("sort",e.getSort());
            resMap.put("icon",e.getIcon());
            resMap.put("isShowStr", EMenuShow.findStatus(Integer.valueOf(e.getIsShow())).getDesc());
            menuMapList.add(resMap);
        }
        return menuMapList;
    }

    /**
     * 菜单树列表
     * @param list
     * @return
     */
    public static List<Map<String, Object>> getMenuSelectList(List<OaSysMenu> list,List<OaSysMenu> menuSystemList){
        List<Map<String, Object>> menuMapList = Lists.newArrayList();
        for (int i=0; i<list.size(); i++){
            OaSysMenu e = list.get(i);
            for(int j=0; j<menuSystemList.size(); j++){
                OaSysMenu sysMenu = menuSystemList.get(j);
                if(e.getMenuId().equals(sysMenu.getMenuId())){
                    Map<String, Object> resMap = Maps.newHashMap();
                    resMap.put("id", e.getMenuId());
                    resMap.put("pId", e.getParentId());
                    resMap.put("name", e.getName());
                    resMap.put("href",e.getHref());
                    resMap.put("sort",e.getSort());
                    resMap.put("icon",e.getIcon());
                    menuMapList.add(resMap);
                }
            }
        }
        return menuMapList;
    }

    /**
     * 区域树列表
     * @param list
     * @return
     */
    public static List<Map<String, Object>> getAreaList(List<OaSysArea> list){
        List<Map<String, Object>> menuMapList = Lists.newArrayList();
        for (int i=0; i<list.size(); i++){
            OaSysArea e = list.get(i);
            Map<String, Object> resMap = Maps.newHashMap();
            resMap.put("id", e.getAreaId());
            resMap.put("pId", e.getParentId());
            resMap.put("name", e.getName());
            resMap.put("adCode", e.getAdCode());
            resMap.put("cityCode", e.getCityCode());
            menuMapList.add(resMap);
        }
        return menuMapList;
    }

    /**
     * 车系品牌列表
     * @param list
     * @return
     */
    public static List<Map<String, Object>> getVehicleMdictList(List<OaSysVehicleMdict> list){
        List<Map<String, Object>> menuMapList = Lists.newArrayList();
        for (int i=0; i<list.size(); i++){
            OaSysVehicleMdict e = list.get(i);
            Map<String, Object> resMap = Maps.newHashMap();
            resMap.put("id", e.getMdictId());
            resMap.put("pId", e.getParentId());
            resMap.put("name", e.getName());
            resMap.put("code", e.getCode());
            resMap.put("vehSeatsNumber", e.getVehSeatsNumber());
            resMap.put("vehicleType", e.getVehicleType());
            menuMapList.add(resMap);
        }
        return menuMapList;
    }



}
